package com.cje.proxyInvocationHandler.JDKDynamicProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Auther: ChenJinEn
 * @Date: 2020/8/8 17:41
 * @Description: //TODO
 */
public class ProxyInvocationHandler implements InvocationHandler {
    //设置被代理的真实对象
    private Object object;

    public void setObject(Object object) {
        this.object = object;
    }

    public ProxyInvocationHandler() {
    }

    public ProxyInvocationHandler(Object object) {
        this.object = object;
    }

    public Object getProxy(){
        return Proxy.newProxyInstance(object.getClass().getClassLoader()
        , object.getClass().getInterfaces(), this);
    }

    //处理代理的具体方法实例,并返回结果
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("收中介费");
        System.out.println("签合同");
        Object result = method.invoke(object, args);
        return result;
    }
}
